<html>
<div id="myheader">
<?php
session_start();
$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name'];
$projectID = $_REQUEST['projectID'];
if($projectID=="")
	$projectID="0";
?>
</div>

<title>OSPMS</title>
<head>
<link rel="stylesheet" href="style/ospms.css" type="text/css">
  <script language="JavaScript" type="text/javascript" src="js/jquery.js"></script>
<style type="text/css" media="all">

#listDiv{width:500px;}
#updateDiv{width:300px; vertical-align:top;}
#middleDiv{ height:500px; width:2px; background:darkgray;}
</style>
</head>
<?php
include "include/dbConfig.php";

session_start();

try {
    $conn = new PDO("mysql:host=$dbservername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	$query = "SELECT study_id, study_name,study_description FROM user_studies where user_id = ". $user_id. " and project_id=".$projectID;
    $stmt = $conn->prepare($query);
    $stmt->execute();
    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
	$array_study = $stmt->fetchAll();

	$query = "SELECT project_id, project_name FROM user_projects where user_id = ". $user_id;
	$stmt = $conn->prepare($query);
	$stmt->execute();
	// set the resulting array to associative
	$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
	$array_project = $stmt->fetchAll();

    $dsn = null;

} // try
catch(PDOException $e)
{
    echo "Error: " . $e->getMessage();
} // try - catch

$conn = null;
?> 


<form method="get" action="addstudy_submit.php">   <!-- default: get; all post most be specified -->
<div id="subMain">

<table><tr>
<td valign="top">
<div id="listDiv">
<?php
echo "<span class='TitleH2'>Project:</span> <select name='projectID' onchange=\"changeClick()\"><option value=''></option>";
foreach($array_project as $key => $oneProject)
{
	echo "<option value='".$oneProject['project_id']."'>".$oneProject['project_name']."</option>";
}
echo "</select><br/>";


$num=0;
echo "<table border='1'cellpadding='5' cellspacing='5' class='DataTable'>";
echo "<tr><th>Study Name</th><th>Study Description</th><th>Edit</th></tr>";
foreach($array_study as $key => $oneStudy)
{
	$num++;
	if($num%2==1)
		echo "<tr class='tableData'>";
	else 
		echo "<tr class='tableData2'>";
	echo "<td>". $oneStudy['study_name']."</td>";
	echo "<td>". $oneStudy['study_description']."</td>";
	echo "<td><a href='javascript:void()' onclick=\"clickUpdate('".$oneStudy['study_id']."','".$oneStudy['study_name']."','".$oneStudy['study_description']."')\">Edit</a>
	/<a   href='javascript:void()' onclick=\"clickDelete('".$oneStudy['study_id']."','".$oneStudy['study_name']."','".$oneStudy['study_description']."')\">Del</a></td>";
	echo "</tr>";
	
}
echo "</table>"

?>
</div>
</td>
<td valign="middle">
<div id="middleDiv"></div>
</td>
<td valign="top">
<div id="updateDiv">
<table class="editTable" >

	<tr>
      <td class="lefttd">Name:</td>
      <td class="righttd"><input type="text" name="studyName" value=""  class="text"/></td>
    </tr>
    <tr>
      <td class="lefttd">Description:</td>
      <td class="righttd"><input type="text" name="studyDescription" class="text"/></td>
    </tr>
    <tr>
	<td></td>
      <td class="righttd" >
	<input type="hidden" name="studyID" value="">
        <input type="submit" value="Update/Add Project" class="ActionButton" name="cmdLogin" id="cmdLogin"/>
      </td>
    </tr>
</table>
</div>
</td>
</tr></table>
</div>
</form>

<script>
function clickUpdate(id,projectName,projectDescription)
	{
		$("[name='studyName']").val(projectName);
		$("[name='studyDescription']").val(projectDescription);
		$("[name='studyID']").val(id);

	}
	function clickDelete(id,projectName,projectDescription)
	{
		window.location.href="addstudy_submit.php?actionType=Del&studyID="+id+"&projectID="+$("[name='projectID']").val();

	}
	function changeClick()
	{
		window.location="/OSPMS/Study.php?projectID="+$("[name='projectID']").val();
	}
	
	$("[name='projectID']").val("<?php echo "$projectID"; ?>");
	$("#listDiv").css("height",$(document.body).height()-10);
</script>
